import moment from 'moment'
import 'moment/locale/zh-cn'
import { NOTICE_KEY } from '@/utils/const'
import { notification } from 'ant-design-vue'
import storage from 'store'
import { getResourceList } from '@/api/user'

export function getTimeStamp () {
  return moment().valueOf()
}

export function getTimeFormatYYYYMMDDHHMMSS (time) {
  return moment(time).format('YYYYMMDDHHmmss')
}

export function timeFix () {
  const time = new Date()
  const hour = time.getHours()
  return hour < 9 ? '早上好' : hour <= 11 ? '上午好' : hour <= 13 ? '中午好' : hour < 20 ? '下午好' : '晚上好'
}

export const getNowFormatDate = (
  date = new Date(),
  formate = 'yyyyMMddHHmmss'
) => {
  const seperator1 = '-'
  const seperator2 = ':'
  let month = date.getMonth() + 1
  let strDate = date.getDate()
  if (month >= 1 && month <= 9) {
    month = '0' + month
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = '0' + strDate
  }

  var strHour = date.getHours()
  var strMinutes = date.getMinutes()
  var strSeconds = date.getSeconds()
  if (strHour >= 1 && strHour <= 9) {
    strHour = '0' + strHour
  }
  if (strMinutes >= 1 && strMinutes <= 9) {
    strMinutes = '0' + strMinutes
  }
  if (strSeconds >= 1 && strSeconds <= 9) {
    strSeconds = '0' + strSeconds
  }

  let currentdate = ''
  switch (formate) {
    case 'yyyy-MM':
      currentdate = date.getFullYear() + seperator1 + month
      break
    case 'yyyy-MM-dd':
      currentdate =
        date.getFullYear() + seperator1 + month + seperator1 + strDate
      break
    case 'yyyy-MM-dd HH:mm:ss':
      currentdate =
        date.getFullYear() +
        seperator1 +
        month +
        seperator1 +
        strDate +
        ' ' +
        strHour +
        seperator2 +
        strMinutes +
        seperator2 +
        strSeconds
      break
    case 'yyyyMMddHHmmss':
      currentdate =
        date.getFullYear() +
        month.toString() +
        strDate.toString() +
        strHour.toString() +
        strMinutes.toString() +
        strSeconds.toString()
      break
  }
  return currentdate
}

export function getLastMonth () {
  const startDate = moment().month(moment().month() - 1).startOf('month')
  const endDate = moment().month(moment().month() - 1).endOf('month')
  return [startDate, endDate]
}

export function getLastYear () {
  const startDate = moment().month(moment().month() - 12).startOf('month')
  const endDate = moment().month(moment().month() - 1).endOf('month')
  return [startDate, endDate]
}

// 失败处理
export function failHandler (res) {
  notification.warning({
    key: NOTICE_KEY,
    message: '错误',
    description: res.retMsg
  })
}

// 错误处理
export function errorHandler (error) {
  notification.error({
    key: NOTICE_KEY,
    message: '错误',
    description: '后台接口错误,error: ' + error
  })
}

export async function calcIndexRoutePath () {
  const userInfo = storage.get('USERINFO')
  const params = {
    userId: userInfo.userInfo.userId
  }
  const res = await getResourceList(params)
  const data = res.retObj
  for (let i = 0; i < data.length; i++) {
    if (data[i].meta.show) {
      const route = data[i]
      return route.path ? route.path : route.redirect
    }
  }
}
